﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;

namespace IHISCETeim.DAL
{
    public class AgentPartOrderDAL
    {
        public static int Add(AgentPartOrderInfo obj,List<AgentPartOrderDetailInfo> listParts)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            using (DbConnection conn = DB.CreateConnection())
            {
                //打开连接
                conn.Open();
                //创建事务
                DbTransaction trans = conn.BeginTransaction();
                try
                {
                    int OrderId = 0;

                    DbCommand dbCommand = DB.GetStoredProcCommand("AgentPartOrder_Add");
                    DB.AddInParameter(dbCommand, "AgentName", DbType.String, obj.AgentName);
                    DB.AddInParameter(dbCommand, "AgentId", DbType.Int32, obj.AgentId);
                    DB.AddInParameter(dbCommand, "SerialNumber", DbType.String, obj.SerialNumber);
                    DB.AddInParameter(dbCommand, "MachineType", DbType.String, obj.MachineType);
                    DB.AddInParameter(dbCommand, "CustomerName", DbType.String, obj.CustomerName);
                    DB.AddInParameter(dbCommand, "CustomerMobile", DbType.String, obj.CustomerMobile);
                    DB.AddInParameter(dbCommand, "IntoAddress", DbType.String, obj.IntoAddress);
                    DB.AddInParameter(dbCommand, "FaultDate", DbType.DateTime, obj.FaultDate);
                    DB.AddInParameter(dbCommand, "WorkHour", DbType.Decimal, obj.WorkHour);
                    DB.AddInParameter(dbCommand, "OrderTotal", DbType.Decimal, obj.OrderTotal);
                    DB.AddInParameter(dbCommand, "AcceptName", DbType.String, obj.AcceptName);
                    DB.AddInParameter(dbCommand, "AcceptTel", DbType.String, obj.AcceptTel);
                    DB.AddInParameter(dbCommand, "AcceptAddress", DbType.String, obj.AcceptAddress);
                    DB.AddInParameter(dbCommand, "Remark", DbType.String, obj.Remark);
                    DB.AddInParameter(dbCommand, "JHImg", DbType.String, obj.JHImg);
                    DB.AddInParameter(dbCommand, "GSImg", DbType.String, obj.GSImg);
                    DB.AddInParameter(dbCommand, "ChangeBeforImg", DbType.String, obj.ChangeBeforImg);
                    DB.AddInParameter(dbCommand, "ChangeAfertImg", DbType.String, obj.ChangeAfertImg);
                    DB.AddInParameter(dbCommand, "BaseId", DbType.Int32, obj.BaseId);
                    DB.AddInParameter(dbCommand, "OrderState", DbType.String, obj.OrderState);
                    DB.AddInParameter(dbCommand, "ReCheckRemark", DbType.String, obj.ReCheckRemark);
                    OrderId = Convert.ToInt32(DB.ExecuteScalar(dbCommand,trans));

                    foreach (AgentPartOrderDetailInfo itm in listParts)
                    {
                        DbCommand dbCommandDetail = DB.GetStoredProcCommand("AgentPartOrderDetail_Add");
                        DB.AddInParameter(dbCommandDetail, "OrderId", DbType.Int32, OrderId);
                        DB.AddInParameter(dbCommandDetail, "ItemCode", DbType.String, itm.ItemCode);
                        DB.AddInParameter(dbCommandDetail, "Dscription", DbType.String, itm.Dscription);
                        DB.AddInParameter(dbCommandDetail, "Quantity", DbType.Decimal, itm.Quantity);
                        DB.AddInParameter(dbCommandDetail, "Price", DbType.Decimal, itm.Price);
                        DB.AddInParameter(dbCommandDetail, "LineTotal", DbType.Decimal, itm.LineTotal);
                        DB.AddInParameter(dbCommandDetail, "BuyType", DbType.String, itm.BuyType);
                        DB.AddInParameter(dbCommandDetail, "Discount", DbType.Decimal, itm.Discount);
                        DB.AddInParameter(dbCommandDetail, "ShipType", DbType.String, itm.ShipType);
                        DB.AddInParameter(dbCommandDetail, "LineRemark", DbType.String, itm.LineRemark);
                        DB.AddInParameter(dbCommandDetail, "OldPrice", DbType.Decimal, itm.OldPrice);
                        DB.ExecuteScalar(dbCommandDetail, trans);
                    }
                    trans.Commit();
                    return OrderId;
                }
                catch (Exception exc)
                {
                    SEMailListDAL.AddAppError(string.Format("类方法:{0}<BR/> 错误:{1}", "AgentPartOrderDAL.Add", exc.Message));
                    //发生异常，事务回滚
                    trans.Rollback();
                    return 0;
                }
                finally
                {
                    //关闭连接
                    conn.Close();
                }
            }
        }
        public static bool Delete(int OrderId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("AgentPartOrder_Delete", OrderId);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static IDataReader GetList(int OrderId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("AgentPartOrder_GetList", OrderId);
            return DB.ExecuteReader(dbCommand);
        }
        public static DataTable GetPagination(string whereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("AgentPartOrder_GetPagination");
            DB.AddInParameter(dbCommand, "whereSql", DbType.String, whereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String, Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32, 8);
            DataTable dt = DB.ExecuteDataSet(dbCommand).Tables[0];

            TotalCount = Convert.ToInt32(DB.GetParameterValue(dbCommand, "TotalCount"));
            return dt;
        }
        public static bool Update(AgentPartOrderInfo obj, List<AgentPartOrderDetailInfo> listParts)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            using (DbConnection conn = DB.CreateConnection())
            {
                //打开连接
                conn.Open();
                //创建事务
                DbTransaction trans = conn.BeginTransaction();
                try
                {

                    DbCommand dbCommandDelete = DB.GetStoredProcCommand("AgentPartOrderDetail_Delete");
                    DB.AddInParameter(dbCommandDelete, "OrderId", DbType.Int32, obj.OrderId);
                    DB.ExecuteScalar(dbCommandDelete, trans);

                    DbCommand dbCommand = DB.GetStoredProcCommand("AgentPartOrder_Update");
                    DB.AddInParameter(dbCommand, "OrderId", DbType.Int32, obj.OrderId);
                    DB.AddInParameter(dbCommand, "SerialNumber", DbType.String, obj.SerialNumber);
                    DB.AddInParameter(dbCommand, "MachineType", DbType.String, obj.MachineType);
                    DB.AddInParameter(dbCommand, "CustomerName", DbType.String, obj.CustomerName);
                    DB.AddInParameter(dbCommand, "CustomerMobile", DbType.String, obj.CustomerMobile);
                    DB.AddInParameter(dbCommand, "IntoAddress", DbType.String, obj.IntoAddress);
                    DB.AddInParameter(dbCommand, "FaultDate", DbType.DateTime, obj.FaultDate);
                    DB.AddInParameter(dbCommand, "WorkHour", DbType.Decimal, obj.WorkHour);
                    DB.AddInParameter(dbCommand, "OrderTotal", DbType.Decimal, obj.OrderTotal);
                    DB.AddInParameter(dbCommand, "AcceptName", DbType.String, obj.AcceptName);
                    DB.AddInParameter(dbCommand, "AcceptTel", DbType.String, obj.AcceptTel);
                    DB.AddInParameter(dbCommand, "AcceptAddress", DbType.String, obj.AcceptAddress);
                    DB.AddInParameter(dbCommand, "Remark", DbType.String, obj.Remark);
                    DB.AddInParameter(dbCommand, "JHImg", DbType.String, obj.JHImg);
                    DB.AddInParameter(dbCommand, "GSImg", DbType.String, obj.GSImg);
                    DB.AddInParameter(dbCommand, "ChangeBeforImg", DbType.String, obj.ChangeBeforImg);
                    DB.AddInParameter(dbCommand, "ChangeAfertImg", DbType.String, obj.ChangeAfertImg);
                    DB.AddInParameter(dbCommand, "UpdateDate", DbType.String, obj.UpdateDate);
                    DB.AddInParameter(dbCommand, "OrderState", DbType.String, obj.OrderState);
                    DB.AddInParameter(dbCommand, "ReCheckRemark", DbType.String, obj.ReCheckRemark);
                    DB.ExecuteScalar(dbCommand, trans);


                    foreach (AgentPartOrderDetailInfo itm in listParts)
                    {
                        DbCommand dbCommandDetail = DB.GetStoredProcCommand("AgentPartOrderDetail_Add");
                        DB.AddInParameter(dbCommandDetail, "OrderId", DbType.Int32, obj.OrderId);
                        DB.AddInParameter(dbCommandDetail, "ItemCode", DbType.String, itm.ItemCode);
                        DB.AddInParameter(dbCommandDetail, "Dscription", DbType.String, itm.Dscription);
                        DB.AddInParameter(dbCommandDetail, "Quantity", DbType.Decimal, itm.Quantity);
                        DB.AddInParameter(dbCommandDetail, "Price", DbType.Decimal, itm.Price);
                        DB.AddInParameter(dbCommandDetail, "LineTotal", DbType.Decimal, itm.LineTotal);
                        DB.AddInParameter(dbCommandDetail, "BuyType", DbType.String, itm.BuyType);
                        DB.AddInParameter(dbCommandDetail, "Discount", DbType.Decimal, itm.Discount);
                        DB.AddInParameter(dbCommandDetail, "ShipType", DbType.String, itm.ShipType);
                        DB.AddInParameter(dbCommandDetail, "LineRemark", DbType.String, itm.LineRemark);
                        DB.AddInParameter(dbCommandDetail, "OldPrice", DbType.Decimal, itm.OldPrice);
                        DB.ExecuteScalar(dbCommandDetail, trans);
                    }

                    trans.Commit();
                    return true;
                }
                catch (Exception exc)
                {
                    SEMailListDAL.AddAppError(string.Format("类方法:{0}<BR/> 错误:{1}", "AgentPartOrderDAL.Update", exc.Message));
                    //发生异常，事务回滚
                    trans.Rollback();
                    return false;
                }
                finally
                {
                    //关闭连接
                    conn.Close();
                }
            }

        }
       
        public static IDataReader GetAllDetailByOrderId(int OrderId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("AgentPartOrderDetail_GetAllByOrderId", OrderId);
            return DB.ExecuteReader(dbCommand);
        }

        /// <summary>
        /// 根据用户，获取最后一次配件收件信息
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public static DataTable GetLastAcceptInfo(int UserId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("AgentPartOrder_GetLastAcceptInfo", UserId);
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }

        public static bool Cancel(int OrderId)
        {
           // Database DB = DatabaseFactory.CreateDatabase();

         
            //return DB.ExecuteDataSet(dbCommand).Tables[0];

            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("AgentPartOrder_Cancel", OrderId);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool ReCheck(int OrderId, string ReCheckRemark, string OrderState)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("AgentPartOrder_ReCheck", OrderId, ReCheckRemark, OrderState);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }

        }
        /// <summary>
        /// 代理商保外服务支持配件销售明细
        /// </summary>
        /// <param name="sqlwhere"></param>
        /// <returns></returns>
        public static DataTable GetPartListReport(string sqlwhere)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("AgentPartOrder_GetPartListReport", sqlwhere);
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }
    }
}
